/**
 * Registration.java
 *
 * Created on Mar 7, 2010, 8:32:41 PM
 * 
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */

package unibo.lsb.graphic;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import unibo.lsb.communication.RegistrationImpl;
import unibo.lsb.exception.TokioException;
import unibo.lsb.server.ConfigurationConstants;

public class CreateGameWindow extends javax.swing.JFrame {

	private String serverAddress = "";

	/** Creates new form Registration */
	public CreateGameWindow() {
		initComponents();
	}

	CreateGameWindow(JFrame jFrame) {
		initComponents();
	}

	private void initComponents() {

		setTitle("Create new game");
		setSize(new Dimension(450, 170));
		setLocationRelativeTo(null);

		btn_ok = new javax.swing.JButton();
		btn_cancel = new javax.swing.JButton();
		lbl_gameName = new javax.swing.JLabel();
		txt_gameName = new javax.swing.JTextField();
		lbl_playerNumber = new javax.swing.JLabel();
		cmb_playerNumber = new javax.swing.JComboBox();
		lbl_serverAddress = new javax.swing.JLabel();
		cb_serverAddresses = new javax.swing.JComboBox(
				ConfigurationConstants.POSSIBLE_SERVERS);

		// setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				exitWindow();
			}
		});

		cb_serverAddresses.setEditable(true);
		cb_serverAddresses.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				serverAddress = (String) cb_serverAddresses.getSelectedItem();
			}
		});

		btn_ok.setText("OK");
		btn_ok.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mousePressed(java.awt.event.MouseEvent evt) {
				btn_createGamePressed(evt);
			}
		});
		btn_cancel.setText("Exit");
		btn_cancel.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mousePressed(java.awt.event.MouseEvent evt) {
				exitWindow();
			}
		});

		lbl_gameName.setText("Game Name");

		lbl_playerNumber.setText("Players");

		cmb_playerNumber.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "2", "3", "4", "5", "6", "7", "8" }));

		lbl_serverAddress.setText("Server Address");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);

		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																lbl_gameName)
														.addComponent(
																lbl_playerNumber))
										.addGap(31, 31, 31)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																txt_gameName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																299,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cmb_playerNumber,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap())
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addContainerGap(163, Short.MAX_VALUE)
										.addComponent(
												btn_ok,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												97,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(10)
										.addComponent(
												btn_cancel,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												97,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(110))
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(lbl_serverAddress)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												cb_serverAddresses,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												299,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_serverAddress)
														.addComponent(
																cb_serverAddresses,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				lbl_gameName)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				lbl_playerNumber))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				txt_gameName,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				cmb_playerNumber,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addGap(23, 23, 23).addGroup(
												layout.createParallelGroup()
														.addComponent(btn_ok)
														.addGap(32)
														.addComponent(
																btn_cancel))));
	}

	protected void btn_createGamePressed(MouseEvent evt) {
		RegistrationImpl ri;
		boolean err = false;
		try {
			if (serverAddress.equals("")) {
				serverAddress = (String) cb_serverAddresses.getSelectedItem();
			}
			ri = new RegistrationImpl(serverAddress);
			int item = Integer.valueOf((String) cmb_playerNumber
					.getItemAt(cmb_playerNumber.getSelectedIndex()));
			ri.createGameOnServer(txt_gameName.getText(), item);
		} catch (TokioException e) {
			err = true;
			e.DisplayErrorInDialog(this);
		}

		if (!err) {
			exitWindow();
		}
		// else {
		// System.err.println("Err");
		// }
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CreateGameWindow().setVisible(true);
			}
		});
	}

	private void exitWindow() {
		MainWindowSingleton.getInstance().getMainWindow().setVisible(true);
		this.setVisible(false);
		// GraphicsTools.getInstance().getCreateGame().setVisible(false);
	}

	private javax.swing.JButton btn_ok;
	private javax.swing.JButton btn_cancel;
	private javax.swing.JComboBox cmb_playerNumber;
	private javax.swing.JLabel lbl_gameName;
	private javax.swing.JLabel lbl_playerNumber;
	private javax.swing.JLabel lbl_serverAddress;
	private javax.swing.JTextField txt_gameName;
	private javax.swing.JComboBox cb_serverAddresses;

}
